﻿using System;
using System.Linq;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using GUAPAddin.Common;
using System.IO;
using AutoUnitTest.Common;
using AutoUnitTest.Common.CodeGeneration;
using AutoUnitTest.Common.CodeAdapter;
using AutoUnitTest.Common.CodeGeneration.CS;

namespace AutoUnitTest.UI
{
    public partial class CodeGen : Form
    {
        private readonly DTE2 _applicationObject;
        private Graph _graph;
        private Vertex _selectedVertex;
        private Edge _selectedEdge;
        ListViewGroup _listViewGroup1;
        

        public CodeGen(DTE2 applicationObject)
        {
            InitializeComponent();
            Load += CodeGen_Load;
            _applicationObject = applicationObject;
            _listViewGroup1 = new ListViewGroup("CLASSES", HorizontalAlignment.Left);
            listView1.Groups.Add(_listViewGroup1);
            listView1.ItemSelectionChanged += listView1_ItemSelectionChanged;
            
        }

        void CodeGen_Load(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            AnalyzeProcess process = new AnalyzeProcess();
            process.StartAnalyze(_applicationObject);
            _graph = process.getGraph();



            foreach (Vertex v in _graph.VertexList.Where(v => v.Type.Name == "class"))
                listView1.Items.Add(new ListViewItem(v.Name, _listViewGroup1));
        }

        void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            var mockgen = new GraphToCodeConvertor(_graph);
            txtMock.Text = mockgen.GetGeneratedClass(e.Item.Text, new MockGenerator());

            var testgen = new GraphToCodeConvertor(_graph);
            txtTest.Text = testgen.GetGeneratedClass(e.Item.Text, new MsTestGenerator());
        }

        private void OnAddMockClick(object sender, EventArgs e)
        {
            string fileNameToGenerate = "Mock" + listView1.SelectedItems[0].Text.Type();
            StreamWriter streamWriter = CreateProjectFile("Mocks", fileNameToGenerate);
            streamWriter.Write(txtMock.Text);
            streamWriter.Close();
        }

        private StreamWriter CreateProjectFile(string folderName, string fileName)
        {
            Solution2 solution = _applicationObject.Solution as Solution2;

            Project pr = solution.Item(1);

            var items = pr.ProjectItems.Cast<ProjectItem>();


            ProjectItem folder = items.Where(i => i.Name == folderName).Count() == 0
                                     ? pr.ProjectItems.AddFolder(folderName)
                                     : items.First(i => i.Name == folderName);
            

            string path = (string)folder.Properties.Item("FullPath").Value;

            string filePath = path + fileName + ".cs";

            StreamWriter sw =   File.CreateText(filePath);

            folder.ProjectItems.AddFromFile(filePath);

            pr.Save();

            return sw;
        }

        private void OnAddTestClick(object sender, EventArgs e)
        {
            string fileNameToGenerate = listView1.SelectedItems[0].Text.Type() + "Test"; 
            StreamWriter streamWriter = CreateProjectFile("Tests", fileNameToGenerate);
            streamWriter.Write(txtTest.Text);
            streamWriter.Close();
        }
    }
}
